package com.edgclub.edg.uc.galleryviewpager;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.AsyncTask;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView.ScaleType;

import com.edgclub.edg.R;
import com.edgclub.edg.activity.GzImageGalleryViewPagerTopicConfirmActivity;
import com.edgclub.edg.utils.util.BitmapUtility;
import com.edgclub.edg.utils.util.StringUtils;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;

public class UrlPagerAdapter extends BasePagerAdapter {

	HashMap<Integer, UrlTouchImageView> map;
	
	public UrlPagerAdapter(Context context, ArrayList<String> resources) {
		super(context, resources);
		map = new HashMap<Integer, UrlTouchImageView>();
	}

	@Override
	public void setPrimaryItem(ViewGroup container, final int position,
			Object object) {
		super.setPrimaryItem(container, position, object);
		try {
			((GalleryViewPager) container).mCurrentView = ((UrlTouchImageView) object)
					.getImageView();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public Object instantiateItem(ViewGroup collection, final int position) {
		UrlTouchImageView iv;
		
		if (map.containsKey(position)){
			 iv = map.get(position);
		} else {
			 iv = new UrlTouchImageView(
					mContext);
			 map.put(position, iv);
		}
		
		// imageLoader.displayImage(mResources.get(position), iv.getImageView(),
		// options);
		Log.i("test", position +"-----instantiateItem------"  + mResources.get(position));

		if (mResources.get(position).endsWith("gif")){
			iv.setUrl(mResources.get(position));
		} else {
			if (mResources.get(position).startsWith("file")) {
				new LoadBitmapTask(iv, mResources.get(position).replace("file://", ""))
						.execute();
			} else {
				iv.setUrl(mResources.get(position));
			}
		}

		iv.setLayoutParams(new ViewGroup.LayoutParams(
				ViewGroup.LayoutParams.MATCH_PARENT,
				ViewGroup.LayoutParams.MATCH_PARENT));

		collection.addView(iv, 0);

		return iv;
	}

	@Override
	public void destroyItem(ViewGroup collection, int position, Object view) {
		if (map.containsKey(position)){
			UrlTouchImageView v = map.get(position);
			BitmapDrawable drawable = (BitmapDrawable) v.mImageView
					.getDrawable();
			if (drawable != null){
				Bitmap bmp = drawable.getBitmap();
				if (null != bmp && !bmp.isRecycled()) {
					bmp.recycle();
					bmp = null;
				}
			}
			v.mImageView.setVisibility(View.GONE);
			v.tvProgress.setVisibility(View.VISIBLE);
			map.remove(position);
			v = null;
			collection.removeView((View) view);
			view = null;
		}
	}

	public interface onItemTouchImageViewClickListener {
		public void onClick(UrlTouchImageView iv);
	}
	
	public void destory(){
		try {
			for (int i : map.keySet()) {
                UrlTouchImageView v = map.get(i);
                BitmapDrawable drawable = (BitmapDrawable) v.mImageView
                        .getDrawable();
                if (drawable != null){
                    Bitmap bmp = drawable.getBitmap();
                    if (null != bmp && !bmp.isRecycled()) {
                        bmp.recycle();
                        bmp = null;

                    }
                }
            }
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public HashMap<Integer, UrlTouchImageView> getMap() {
		return map;
	}

	public void setMap(HashMap<Integer, UrlTouchImageView> map) {
		this.map = map;
	}
	
	private class LoadBitmapTask extends AsyncTask<Void, Void, Bitmap> {

		private UrlTouchImageView iv;

		private String path;

		public LoadBitmapTask(UrlTouchImageView iv, String path1) {
			super();
			this.iv = iv;
			if(path1.lastIndexOf(GzImageGalleryViewPagerTopicConfirmActivity.tag)!=-1)
				this.path=path1.substring(0,path1.lastIndexOf(GzImageGalleryViewPagerTopicConfirmActivity.tag));
			else
				this.path=path1;
		}

		@Override
		protected void onPostExecute(Bitmap result) {
			super.onPostExecute(result);
			if (result != null) {

				try {
					iv.mImageView.setVisibility(View.VISIBLE);
					iv.tvProgress.setVisibility(View.GONE);
					iv.mImageView.setScaleType(ScaleType.MATRIX);
					iv.mImageView.setImageBitmap(result);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
					iv.mImageView.setScaleType(ScaleType.CENTER);
					iv.tvProgress.setText(StringUtils.getString(R.string.imageloadingcontent1));
				}
			} else {
				iv.mImageView.setScaleType(ScaleType.CENTER);
				iv.tvProgress.setText(StringUtils.getString(R.string.imageloadingcontent1));
			}
		}

		@Override
		protected void onPreExecute() {
			iv.mImageView.setScaleType(ScaleType.CENTER);
			iv.tvProgress.setText(StringUtils.getString(R.string.imageloadingcontent2));
			super.onPreExecute();
		}

		@Override
		protected Bitmap doInBackground(Void... arg0) {
			Bitmap bitmap = null;
			try {
				bitmap = BitmapUtility.getBitmapFromUri2(mContext,
						Uri.fromFile(new File(path)));
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			return bitmap;
		}

	}
	
}
